<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Article extends Base
{

    // 首页方法
    public function index(Request $request){

        $res = $request->param();

        //所有的分类数据
        $arrs = \app\admin\model\Category::select();

        // 获取无线级分类
        $category = new \app\admin\model\Category();
        $cate = $category->categoryList($arrs);

        // 判断数据当中是否存在值
        if(!empty($res['keyword']) || !empty($res['category_id'])){

            $id = $res['category_id'] ? $res['category_id'] : 'null';
            $key = $res['keyword'];

            $art = new \app\admin\model\Article();

            $data = $art->AtticleKey($id,$key);
        }else{
            // 所有的文章数据
            $art = new \app\admin\model\Article();
            $data = $art->ArticleList();
        }

        // 根据文章数据id查询分类
        return view('index',['data'=>$data,'cate'=>$cate]);
    }

    // 添加文章页
    public function add(){

        //所有的分类数据
        $arrs = \app\admin\model\Category::select();

        // 获取无线级分类
        $category = new \app\admin\model\Category();

        $data = $category->categoryList($arrs);


        return view("add",['data'=>$data]);
    }

    // 添加文章到数据库
    public function insert(Request $request)
    {
        $data = $request->param();
        if($data['category_id'] == 0){
            $this->error("文章分类id不能为不写",'admin/article/add');
        }
        $rule = [
            'category_id|分类id' => 'require',
            'title|文章名称' => 'require|min:5',
            'orderby|排序' => 'require',
            'content|内容' => 'require|min:5'
        ];

        $validate = new \think\Validate($rule);

        if(!$validate->check($data))
        {
            $error = $validate->getError();
            $this->error($error);
        }

        if(empty($data))
        {
            return $this->error('错误信息','admin/article/index');
        }

        $data['user_id'] = session('msg_info.id');
        $data['addate'] = time();
        \app\admin\model\Article::strict(false)->insert($data);

        return $this->success("添加成功",'admin/article/index');
    }

    // 修改方法
    public function edit(int $id)
    {
        if(empty($id))
        {
            return $this->error('出现错误','admin/article/index');
        }

        // 获取所有文章信息
        $art = new \app\admin\model\Article();
        $Article = $art->oneArt($id);
        // 分类信息
        $arrs = \app\admin\model\Category::select();
        $cate = new \app\admin\model\Category();
        $data = $cate->categoryList($arrs);

        // 获取修改休息
        return view('/article/edit',['Article'=>$Article,'data'=>$data]);
    }

    // 修改添加到数据库
    public function update(Request $request,int $id)
    {
        if(empty($id)){

            return $this->error('错误哦','admin/article/index');
        }

        $data = $request->param();

        \app\admin\model\Article::where('id',$id)->strict(false)->update($data);

        return $this->success("修改成功",'admin/article/index');

    }

    // 删除
    public function delete(int $id)
    {
        if(empty($id))
        {
            return $this->error("出错了",'admin/article/index');
        }

        \app\admin\model\Article::strict(false)->where('id',$id)->delete();

        return $this->success("删除成功",'admin/article/index');
    }

}
